package org.mongo.template.repository.impl;

import org.mongo.template.entity.PersonEntity;
import org.mongo.template.enums.SexEnum;
import org.mongo.template.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * @author qilongxin
 * @date 2021/8/1 16:35
 */
@Repository
public class PersonRepositoryImpl implements PersonRepository {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PersonEntity findDocuments(Integer pid) {
        Query query = new Query();
        query.addCriteria(Criteria.where(PersonEntity.Fields.pid).is(pid));
        PersonEntity personEntity = mongoTemplate.findOne(query, PersonEntity.class, "person");
        // assert personEntity != null;
        return personEntity;
    }

    @Override
    public List<PersonEntity> findDocuments(SexEnum sex, Integer age) {
        Query query = new Query();
        query.addCriteria(Criteria.where(PersonEntity.Fields.sex).is(sex).and(PersonEntity.Fields.age).gte(age));
        return mongoTemplate.find(query, PersonEntity.class, "person");
    }

    @Override
    public void addDocuments(List<PersonEntity> personEntityList) {
        mongoTemplate.insertAll(personEntityList);
    }

    @Override
    public void updateDocuments(Integer name, Integer age) {
        Query query = new Query();
        query.addCriteria(Criteria.where(PersonEntity.Fields.name).is(name));
        Update update = new Update();

    }

    @Override
    public void updateDocuments(Integer name, String email) {
        Query query = new Query();
        query.addCriteria(Criteria.where(PersonEntity.Fields.name).is(name));
        Update update = new Update();

    }

    @Override
    public void deleteDocuments(Integer pid) {

    }
}
